package com.mars.common.component.redis.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.JedisCluster;

import com.mars.common.component.redis.JedisClient;
import com.mars.common.utils.ProtoStuffSerializerUtils;

public class JedisClientCluster implements JedisClient {
	
	private static Logger logger = LoggerFactory.getLogger(JedisClientSingle.class) ;
	
	/**
	 * 通过Spring声明式方式注入JedisCluster
	 */
	private JedisCluster jedisCluster ;
	
	public JedisCluster getJedisCluster() {
		return jedisCluster;
	}

	public void setJedisCluster(JedisCluster jedisCluster) {
		this.jedisCluster = jedisCluster;
	}
	
	@Override
	public void set(String key, String value) {
		try {
			jedisCluster.set(key, value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		}
	}
	
	@Override
	public String get(String key) {
		try {
			return jedisCluster.get(key) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		}
	}
	
	@Override
	public boolean del(String key) {
		try {
			jedisCluster.del(key) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		}
	}
	@Override
	public void set(byte[] key, byte[] value) {
		try {
			jedisCluster.set(key, value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		}
	}

	@Override
	public byte[] get(byte[] key) {
		try {
			return jedisCluster.get(key) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		}
	}

	@Override
	public boolean del(byte[] key) {
		try {
			jedisCluster.del(key) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		}
	}

	@Override
	public void setHash(String key, String field, String value) {
		try {
			jedisCluster.hset(key, field, value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		}
	}

	@Override
	public String getHash(String key, String field) {
		try {
			return jedisCluster.hget(key, field) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		}
	}

	@Override
	public boolean delHash(String key, String field) {
		try {
			jedisCluster.hdel(key, field) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		}
	}

	@Override
	public void setHash(String key, String field, byte[] value) {
		try {
			jedisCluster.hset(serialize(key), serialize(field), value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		}
	}

	@Override
	public byte[] getHashByte(String key, String field) {
		try {
			return jedisCluster.hget(serialize(key), serialize(field)) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		}
	}

	@Override
	public boolean delHashByte(String key, String field) {
		try {
			jedisCluster.hdel(serialize(key), serialize(field)) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		}
	}
	
	@Override
	public Long expire(String key, int seconds) {
		try {
			return jedisCluster.expire(key.toString(), seconds) ;
		} catch(Exception e) {
			logger.error("设置缓存失效时间失败[key：{}]", key, e);
			return null ;
		}
	}
	
	@Override
	public Long expire(byte[] key, int seconds) {
		try {
			return jedisCluster.expire(key, seconds) ;
		} catch(Exception e) {
			logger.error("设置缓存失效时间失败[key：{}]", key, e);
			return null ;
		}
	}
	
	@Override
	public Long ttl(String key) {
		try {
			return jedisCluster.ttl(key.toString()) ;
		} catch(Exception e) {
			logger.error("返回剩余生存时间失败[key：{}]", key, e);
			return null ;
		}
	}
	
	@Override
	public Long ttl(byte[] key) {
		try {
			return jedisCluster.ttl(key) ;
		} catch(Exception e) {
			logger.error("返回剩余生存时间失败[key：{}]", key, e);
			return null ;
		}
	}
	
	@Override
	public Boolean exists(String key, int seconds) {
		try {
			return jedisCluster.exists(key) ;
		} catch(Exception e) {
			logger.error("查询缓存是否存在失败[key：{}]", key, e);
			return null ;
		}
	}

	@Override
	public Boolean exists(byte[] key, int seconds) {
		try {
			return jedisCluster.exists(key) ;
		} catch(Exception e) {
			logger.error("查询缓存是否存在失败[key：{}]", key, e);
			return null ;
		}
	}
	
	@Override
	public void flushAll() {
		
	}
	
	protected byte[] serialize(Object obj) {
		return ProtoStuffSerializerUtils.serialize(obj);
	}

	protected byte[] serializeList(List<Object> objList) {
		return ProtoStuffSerializerUtils.serializeList(objList) ;
	}

	@Override
	public void incr(String key) {
		// TODO Auto-generated method stub
		
	}


}
